﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Runtime.Serialization;

namespace Dos.Common
{
    [DataContract]
    public class JResult : DynamicObject
    {
        public Dictionary<string, object> Properties = new Dictionary<string, object>();

        [DataMember]
        public bool success
        {
            get;
            set;
        }

        [DataMember]
        public string message
        {
            get;
            set;
        }

        [DataMember]
        public object data
        {
            get;
            set;
        }

        [DataMember]
        public int? datacount
        {
            get;
            set;
        }

        public JResult()
        {
            this.success = true;
            this.data = null;
            this.message = null;
        }

        public JResult(bool isSuccess)
        {
            this.success = isSuccess;
            this.data = null;
            this.message = null;
        }

        public JResult(bool isSuccess, object data)
        {
            this.success = isSuccess;
            this.data = data;
            this.message = null;
        }

        public JResult(bool isSuccess, object data, string message = "")
        {
            this.success = isSuccess;
            this.data = data;
            this.message = message;
        }

        public JResult(bool isSuccess, object data, string message = "", int dataCount = 0)
        {
            this.success = isSuccess;
            this.data = data;
            this.message = message;
            this.datacount = new int?(dataCount);
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (!this.Properties.Keys.Contains(binder.Name))
            {
                this.Properties.Add(binder.Name, value.ToString());
            }
            return true;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return this.Properties.TryGetValue(binder.Name, out result);
        }
    }



    
}
